package com.wedo.mall.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.entity.User;
import com.e.library.listener.EListener;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.chat.ConsultActivity;
import com.wedo.mall.util.WUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by e on 2017/9/25.
 */

public class ConversationAdapter extends Adapter<EMConversation> {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public ConversationAdapter(Context context, List<EMConversation> datas) {
        super(context, datas);
    }

    @Override
    protected int layout() {
        return R.layout.item_conversation;
    }

    @Override
    protected void bind(EHolder holder, EMConversation conversation, int position) {

        ImageView iv_img = holder.find(R.id.iv_img);
        TextView tv_title = holder.find(R.id.tv_title);
        TextView tv_time = holder.find(R.id.tv_time);
        TextView tv_content = holder.find(R.id.tv_content);

        EMMessage message = conversation.getLastMessage();
        String digest = EaseCommonUtils.getMessageDigest(message,mContext);
        tv_content.setText(digest);

        long time = message.getMsgTime();
        String date = format.format(new Date(time));
        tv_time.setText(date);

        if (message.direct() == EMMessage.Direct.RECEIVE){
            display(message,tv_title,iv_img);
        }else {
            EMMessage from = from(conversation);
            if (from !=null){
                display(from,tv_title,iv_img);
            }else {
                tv_title.setText(message.getTo());
                Glide.with(mContext).fromResource().load(R.drawable.ease_default_avatar).into(iv_img);
            }
        }
    }


    private void display(EMMessage message,TextView tv_title,ImageView iv_img){
        User user = EaseCommonUtils.user(message);
        if (user !=null){
            String img = WUrl.url(user.headimgurl);
            String title = user.nickname;
            tv_title.setText(title);
            Glide.with(mContext).load(img)
                    .error(R.drawable.ease_default_avatar)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_img);

        }else {
            tv_title.setText(message.getUserName());
            Glide.with(mContext).fromResource().load(R.drawable.ease_default_avatar).into(iv_img);
        }

    }

    /**遍历获发送者消息，以便可以正常显示头像和昵称*/
    private EMMessage from(EMConversation conversation){
        List<EMMessage> list = conversation.getAllMessages();
        for (EMMessage message:list){
            if (message.direct() == EMMessage.Direct.RECEIVE){
                return message;
            }
        }
        return null;
    }

    public void listener(final EActivity activity){
        onItemClickListener = new EListener<EMConversation>() {
            @Override
            public void onInvoked(View view, EMConversation data, int position) {
                EMMessage message = data.getLastMessage();
                if (message.direct() == EMMessage.Direct.RECEIVE){
                    toConsult(activity,message);
                }else {
                    EMMessage from = from(data);
                    if (from !=null){
                        toConsult(activity,from);
                    }else {
                        User user = new User();
                        user.id = message.getTo();
                        user.nickname = message.getUserName();
                        ConsultActivity.toConsult(activity,user,"");
                    }
                }
            }
        };
    }

    private void toConsult(EActivity activity,EMMessage message){
        User user = EaseCommonUtils.user(message);
        if (user !=null){
            ConsultActivity.toConsult(activity,user,"");
        }else {
            user = new User();
            user.id = message.getFrom();
            user.nickname = message.getUserName();
            ConsultActivity.toConsult(activity,user,"");
        }
    }
}
